import {
  AppstoreAddOutlined,
  BarsOutlined,
  DashboardOutlined,
  FileAddOutlined,
  FolderAddOutlined,
  FontSizeOutlined,
  SettingOutlined,
  TeamOutlined,
  UsergroupAddOutlined,
  UserOutlined,
} from '@ant-design/icons';
import Dashboard from '../pages/Dashboard';
import UserList from '../pages/sys/UserList';
import RoleList from '../pages/sys/RoleList';
import Settings from '../pages/sys/Settings';
import Profile from '../pages/sys/Profile';
import Login from '../pages/Login';
import NotFound from '../pages/error/NotFound';
import Forbidden from '../pages/error/Forbidden';
import React from "react";
import Code from "@/pages/code";
import Template from "@/pages/template";
import Project from "@/pages/project";
import Dev from "@/pages/dev";
import CustomCode from "@/pages/customCode";

export interface RouteConfig {
  path: string;           // 路由路径
  key: string;           // 路由唯一标识
  component: React.ComponentType<any>; // 路由组件
  name: string;          // 路由名称（菜单显示）
  icon?: React.ComponentType<any>;    // 菜单图标
  isMenu?: boolean;      // 是否显示在菜单中
  requireRoles?: string[]; // 需要的角色权限
  children?: RouteConfig[]; // 子路由
}

export const routes = [
  {
    path: '/dashboard',
    key: 'dashboard',
    component: Dashboard,
    name: '仪表盘',
    icon: DashboardOutlined,
    isMenu: true,
  },
  {
    path: '/user',
    key: 'user',
    name: '用户管理',
    icon: TeamOutlined,
    isMenu: true,
    children: [
      {
        path: '/users',
        key: 'users',
        component: UserList,
        name: '用户列表',
        icon: UserOutlined,
        isMenu: true,
        requireRoles: ['admin'],
      },
      {
        path: '/roles',
        key: 'roles',
        component: RoleList,
        name: '角色管理',
        icon: UsergroupAddOutlined,
        isMenu: true,
        requireRoles: ['admin'],
      },
    ],
  },
  {
    path: '/code',
    key: 'code',
    component: Code,
    name: '代码生成',
    icon: FileAddOutlined,
    isMenu: true,
    requireRoles: ['admin','user'],
  },
  {
    path: '/customCode',
    key: 'customCode',
    component: CustomCode,
    name: '自定义代码',
    icon: FolderAddOutlined,
    isMenu: true,
    requireRoles: ['admin','user'],
  },
  {
    path: '/template',
    key: 'template',
    component: Template,
    name: '代码模板',
    icon: FontSizeOutlined,
    isMenu: true,
    requireRoles: ['admin','user'],
  },
  {
    path: '/project',
    key: 'project',
    component: Project,
    name: '项目管理',
    icon: AppstoreAddOutlined,
    isMenu: true,
    requireRoles: ['admin','user'],
  },
  {
    path: '/dev',
    key: 'dev',
    component: Dev,
    name: '功能开发',
    icon: BarsOutlined,
    isMenu: true,
    requireRoles: ['admin','user'],
  },
  {
    path: '/settings',
    key: 'settings',
    component: Settings,
    name: '系统设置',
    icon: SettingOutlined,
    isMenu: true,
    requireRoles: ['admin','user'],
  },
  {
    path: '/profile',
    key: 'profile',
    component: Profile,
    name: '个人信息',
    icon: UserOutlined,
    requireRoles: ['admin','user'],
  },
  {
    path: '/login',
    key: 'login',
    component: Login,
    name: '登录',
  },
  {
    path: '/403',
    key: '403',
    component: Forbidden,
    name: '无权限',
  },
  {
    path: '/404',
    key: '404',
    component: NotFound,
    name: '未找到',
  },
];

// 获取所有需要注册的路由（扁平化处理）
export const getRouteList = (routes: RouteConfig[]): RouteConfig[] => {
  const result: RouteConfig[] = [];
  routes.forEach(route => {
    if (route.children) {
      result.push(...getRouteList(route.children));
    } else {
      result.push(route);
    }
  });
  return result;
};

// 根据用户角色过滤路由
export const filterRoutesByRole = (routes: RouteConfig[], userRole?: string): RouteConfig[] => {
  return routes
    .map(route => {
      if (route.requireRoles && !route.requireRoles.includes(userRole || '')) {
        return null;
      }

      if (route.children) {
        const filteredChildren = filterRoutesByRole(route.children, userRole);
        if (filteredChildren.length === 0) {
          return null;
        }
        return { ...route, children: filteredChildren };
      }

      return route;
    })
    .filter(Boolean) as RouteConfig[];
}; 